<cfparam name="resetFilters" default="false">
<cfscript>
	isFiltered = false;
	queryParams = StructNew();
	statuses = request.Base.getOrderStatuses();
	institutionTypes = request.Base.getInstitutionTypes();
	//approvers = request.UserGateway.query(roles = "Administrator,Manager");
</cfscript>

<!--- 
@orderIDs varchar(max) = NULL,
@approverIDs varchar(max) = NULL,
@institutionIDs varchar(max) = NULL,
@statuses varchar(max) = NULL,
@fromOrderedDate date = NULL,
@toOrderedDate date = NULL,
@fromApprovedDate date = NULL,
@toApprovedDate date = NULL,
@institutionTypes varchar(max) = NULL,
--->

<!--- <cfif NOT IsDefined("client.orders.orderBy") or resetFilters>
	<cfset client.orders.orderBy = "Anticipated_Ship_Date">
<cfelseif IsDefined("orderBy")>
	<cfset client.orders.orderBy = orderBy>
</cfif>

<cfif NOT IsDefined("client.orders.orderDir") or resetFilters>
	<cfset client.orders.orderDir = "asc">
<cfelseif IsDefined("orderDir")>
	<cfset client.orders.orderDir = orderDir>
</cfif> --->

<cfif NOT IsDefined("client.orders.statuses") or resetFilters>
	<cfset client.orders.statuses = "All">
<cfelseif IsDefined("filterStatuses")>
	<cfset client.orders.statuses = filterStatuses>
</cfif>
<cfif client.orders.statuses neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.statuses = client.orders.statuses />
</cfif>

<cfif NOT IsDefined("client.orders.institutionTypes") or resetFilters>
	<cfset client.orders.institutionTypes = "All">
<cfelseif IsDefined("filterInstitutionTypes")>
	<cfset client.orders.institutionTypes = filterInstitutionTypes>
</cfif>
<cfif client.orders.institutionTypes neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.institutionTypes = client.orders.institutionTypes />
</cfif>

<!--- <cfif NOT IsDefined("client.orders.programYear") or resetFilters>
	<cfset client.orders.programYear = request.currentProgramYear>
<cfelseif IsDefined("filterProgramYear")>
	<cfset client.orders.programYear = filterProgramYear>
</cfif>
<cfif client.orders.programYear neq request.currentProgramYear>
	<cfset isFiltered = true />
	<cfset queryParams.Program_Year = client.orders.programYear />
<cfelse>
	<cfset queryParams.Program_Year = request.currentProgramYear />
</cfif>

<cfif NOT IsDefined("client.orders.organizationIDs") or resetFilters>
	<cfset client.orders.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.orders.organizationIDs = filterOrganizationIDs>
</cfif>
<cfif client.orders.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.orders.organizationIDs />
</cfif>

<cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.orders.regionIDs") or resetFilters>
		<cfset client.orders.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.orders.regionIDs = filterRegionIDs>
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.orders.regionIDs = request.authUser.regionID>
</cfif>
<cfif client.orders.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.orders.regionIDs />
</cfif> --->

<cfwddx action="cfml2wddx" input="#queryParams#" output="client.orders.queryParamsWDDX" />

<cfscript>
	orders = request.orderGateway.query(argumentCollection = queryParams);
</cfscript>

<cfoutput>
	
	<div class="row" style="margin-bottom: 10px;">
		<div class="col-lg-12">
			<a href="#script_name#?ui=editOrder&Order_ID=0" class="btn btn-info">New&nbsp;order</a>
			<a id="filterLink" href="##" class="btn btn-info" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn btn-info dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn btn-info dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<li><a href="?action=export&collection=orders" target="_new">Export</a></li>
				</ul>
			</div>
			<span class="pull-right"><span class="label label-primary">#orders.RecordCount# records</span><cfif isFiltered> <span class="label label-warning">Filtered</span></cfif></span>
		</div>
	</div>

	<cfform name="ordersForm" id="ordersForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="ui" value="#ui#">
		
		<div id="filter" class="well well-small" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<!--- <td>
						<label>Program Year</label>
						<cfselect name="filterProgramYear" size="1" multiple="false" required="yes" message="Program Year filter is required" style="width: 150px;">
							<cfloop index="y" from="2008" to="#ListFirst(application.currentProgramYear, '-')#">
								<cfset py = "#y#-" & y+1>
								<option value="#py#"<cfif client.orders.programYear eq py> selected</cfif>>#py#</option>
							</cfloop>
						</cfselect>
					</td>
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.orders.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.orders.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
						</td>
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.orders.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
							<option value="#Organization_ID#"<cfif ListFind(client.orders.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td> --->
					<!--- <td>
						<label>Order Date</label>
						<cfselect name="filterInstitutionTypes" class="form-control" size="5" multiple="true" required="yes" message="Institution Types filter is required">
							<option value="All"<cfif client.orders.institutionTypes eq "All"> selected</cfif>>All</option>
							<cfloop index="i" from="1" to="#ArrayLen(institutionTypes)#">
								<option value="#institutionTypes[i].value#"<cfif ListFind(client.orders.institutionTypes, institutionTypes[i].value) gt 0> selected</cfif>>#institutionTypes[i].text#</option>
							</cfloop>
						</cfselect>
					</td> --->
					<td>
						<label>Institution Types</label>
						<cfselect name="filterInstitutionTypes" class="form-control" size="5" multiple="true" required="yes" message="Institution Types filter is required">
							<option value="All"<cfif client.orders.institutionTypes eq "All"> selected</cfif>>All</option>
							<cfloop index="i" from="1" to="#ArrayLen(institutionTypes)#">
								<option value="#institutionTypes[i].value#"<cfif ListFind(client.orders.institutionTypes, institutionTypes[i].value) gt 0> selected</cfif>>#institutionTypes[i].text#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Statuses</label>
						<cfselect name="filterStatuses" class="form-control" size="5" multiple="true" required="yes" message="Statuses filter is required">
							<option value="All"<cfif client.orders.statuses eq "All"> selected</cfif>>All</option>
							<cfloop index="i" from="1" to="#ArrayLen(statuses)#">
								<option value="#statuses[i].value#"<cfif ListFind(client.orders.statuses, statuses[i].value) gt 0> selected</cfif>>#statuses[i].text#</option>
							</cfloop>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?ui=orders&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif orders.RecordCount gt 0>

			<table class="table table-striped table-bordered table-hover">
				<thead>
					<th>ID</th>
					<th>Order Date</th>
					<th>Institution</th>
					<th>Approved Date</th>
					<th>Items Ordered</th>
					<th>Items Shipped</th>
					<th>Status</th>
				</thead>
				<tbody>
					<cfloop query="orders">
						<tr>
							<td><a href="?ui=order&Order_ID=#Order_ID#">#Order_ID#</a></td>
							<td>#DateFormat(Order_Date, "m/d/yyyy")#</td>
							<td>
								<cfif Institution_Type eq "College University">
									#College_University# - #City#
								<cfelseif Institution_Type eq "High School">
									#High_School# - #City#
								<cfelseif Institution_Type eq "Other">
									#Institution# - #City#
								</cfif>
							</td>
							<td>#DateFormat(Approved_Date, "m/d/yyyy")#</td>
							<td>#Count_Ordered#</td>
							<td>#Count_Shipped#</td>
							<!--- <td>#Quantity_Ordered#</td>
							<td>#Quantity_Shipped#</td> --->
							<td>#Status#</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
		<cfelse>
		
			<div class="alert alert-info">No orders</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
					$("##filterLink").text("Hide filters");
				});
			// -->
			</script>
			
		</cfif>

	</cfform>

</cfoutput>